﻿//-----------------------------------------------------------------------
// <copyright file="ThreadDataContract.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics.Contracts
{
    using System;
    using System.Runtime.Serialization;

    using System.Threading;
    
    /// <summary>
    /// Defines the data contract representing the details of a thread.
    /// </summary>
    [DataContract(Name = "ThreadDataContract", Namespace = "http://schemas.datacontract.org/2004/07/CodePlex.Diagnostics.Contracts")]
    public sealed class ThreadDataContract : IExtensibleDataObject
    {
        #region public ThreadDataContract(Thread thread)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ThreadDataContract"/> class.
        /// </summary>
        /// <param name="thread">The <see cref="T:System.Threading.Thread"/> for which the <see cref="T:CodePlex.Diagnostics.Contracts.ThreadDataContract"/> is required.</param>
        public ThreadDataContract(Thread thread)
        {
            if (thread == null)
            {
                throw new ArgumentNullException("thread");
            }

            this.CurrentCulture = thread.CurrentCulture.ToString();
            
            this.CurrentUICulture = thread.CurrentUICulture.ToString();

            this.IsAlive = thread.IsAlive;

            this.IsBackground = thread.IsBackground;

            this.IsThreadPoolThread = thread.IsThreadPoolThread;

            this.ManagedThreadId = thread.ManagedThreadId;

            this.Name = thread.Name;

            this.ThreadPriority = thread.Priority.ToString();

            this.ThreadState = thread.ThreadState.ToString();
        }

        #endregion

        #region public string CurrentCulture

        /// <summary>
        /// Gets the culture for the current thread.
        /// </summary>
        /// <value>The culture for the current thread.</value>
        [DataMember]
        public string CurrentCulture
        {
            get;
            private set;
        }

        #endregion

        #region public string CurrentUICulture

        /// <summary>
        /// Gets the current culture used by the Resource Manager to look up culture-specific resources at run time.
        /// </summary>
        /// <value>The current culture used by the Resource Manager to look up culture-specific resources at run time.</value>
        [DataMember]
        public string CurrentUICulture
        {
            get;
            private set;
        }

        #endregion

        #region public bool IsAlive

        /// <summary>
        /// Gets a value indicating whether the current thread is alive.
        /// </summary>
        /// <value><c>true</c> if this thread has been started and has not terminated normally or aborted; otherwise, <c>false</c>.</value>
        [DataMember]
        public bool IsAlive
        {
            get;
            private set;
        }

        #endregion

        #region public bool IsBackground

        /// <summary>
        /// Gets a value indicating whether or not a thread is a background thread.
        /// </summary>
        /// <value><c>true</c> if this thread is or is to become a background thread; otherwise, <c>false</c>.</value>
        [DataMember]
        public bool IsBackground
        {
            get;
            private set;
        }

        #endregion

        #region public bool? IsThreadPoolThread

        /// <summary>
        /// Gets a value indicating whether or not a thread belongs to the managed thread pool.
        /// </summary>
        /// <value><c>true</c> if this thread belongs to the managed thread pool; otherwise, <c>false</c>.</value>
        [DataMember]
        public bool? IsThreadPoolThread
        {
            get;
            private set;
        }

        #endregion

        #region public int ManagedThreadId

        /// <summary>
        /// Gets a unique identifier for the current managed thread.
        /// </summary>
        /// <value>An integer that represents a unique identifier for this managed thread.</value>
        [DataMember]
        public int ManagedThreadId
        {
            get;
            private set;
        }

        #endregion

        #region public string Name

        /// <summary>
        /// Gets the name of the thread.
        /// </summary>
        /// <value>A string containing the name of the thread, or <c>null</c> reference (Nothing in Visual Basic) if no name was set.</value>
        [DataMember]
        public string Name
        {
            get;
            private set;
        }

        #endregion

        #region public string ThreadPriority

        /// <summary>
        /// Gets a <see cref="T:String"/> value representing the <see cref="T:System.Threading.ThreadPriority"/> of the current thread.
        /// </summary>
        /// <value>A <see cref="T:String"/> value representing the <see cref="T:System.Threading.ThreadPriority"/> of the current thread.</value>
        [DataMember]
        public string ThreadPriority
        {
            get;
            private set;
        }

        #endregion

        #region public string ThreadState

        /// <summary>
        /// Gets a <see cref="T:String"/> value representing the <see cref="T:System.Threading.ThreadState"/> of the current thread or <c>null</c> for threads executing within the Silverlight runtime.
        /// </summary>
        /// <value>A <see cref="T:String"/> value representing the <see cref="T:System.Threading.ThreadState"/> of the current thread or <c>null</c> for threads executing within the Silverlight runtime.</value>
        [DataMember]
        public string ThreadState
        {
            get;
            private set;
        }

        #endregion

        #region ExtensionDataObject IExtensibleDataObject.ExtensionData

        /// <summary>
        /// Gets or sets the structure that contains extra data.
        /// </summary>
        /// <value>An <see cref="T:System.Runtime.Serialization.ExtensionDataObject"/> that contains data that is not recognized as belonging to the data contract.</value>
        ExtensionDataObject IExtensibleDataObject.ExtensionData
        {
            get;
            set;
        }

        #endregion
    }
}
